package weibo4j.examples.user;

import java.util.LinkedList;
import java.util.List;

import utils.getcode.GetToken;
import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import beans.FriendsStatuses;

/**
 * @author sina
 *
 */
public class GetFriends {
	
	private final int  pageSize;
	
	private Weibo weibo = null;
	
	private AccessToken at = null;
	
	public GetFriends(Weibo weibo,AccessToken at){
//		pageSize = 100;
//		this.weibo = weibo;
		this(weibo,at,100);
	}
	
	public GetFriends(Weibo weibo,AccessToken at,int size){
		pageSize = size;
		this.weibo = weibo;
		this.at = at;
	}
	
	/**
	 * Usage: java -DWeibo4j.oauth.consumerKey=[consumer key] -DWeibo4j.oauth.consumerSecret=[consumer secret] Weibo4j.examples.GetFriends [accessToken] [accessSecret]
	 * @param args message
	 */
	public List<User> getFriends(String weiboId) {
		try {
			
			weibo.setToken(at);
			
			try {

				int cursor = -1;
				List<User> result = new LinkedList<User>();
				FriendsStatuses fs = weibo.getFriendsStatuses2 (weiboId,cursor,pageSize);
				List <User> list = fs.list;
				result.addAll(list);
				int j = 0;
				while (list != null && list.size()>0){
					fs = weibo.getFriendsStatuses2("2193902201",fs.next_cursor,pageSize);
					list = fs.list;
					result.addAll(list);
					j++;
				}

				return result;
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.exit(0);
		} catch (Exception ioe) {
			System.out.println("Failed to read the system input.");
			System.exit( -1);
		}
		
		return null;
	}
}
